package org.example.MySQLToOracle;


import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * ClassName: MysqlDdl
 * Project: json-parsing-data
 * Package: main.MysqlToOracle
 * Description:
 *
 * @Author ljx
 * @Create 2024/12/6 18:42
 * @Version 1.0
 */
public class MysqlDdl {

    private final String url = "jdbc:mysql://192.168.100.4:3306/ads";
    private final String user = "root";
    private final String password = "root@123BY";
    public static StringBuffer buff = new StringBuffer();
    // 获取数据库连接

    private String tableName;
    ResultSet rs = null;
    Connection coon = DriverManager.getConnection(url, user, password);

    public MysqlDdl() throws SQLException {
    }


    public String getTableDdl(String tableName) throws SQLException {
        String ddl = null;
//        DatabaseMetaData meta = coon.getMetaData();
        Statement stmt = coon.createStatement();
        rs = stmt.executeQuery("SHOW CREATE TABLE " + tableName);

        // 检查结果集是否有下一行数据
        if (rs.next()) {
            // 获取当前行的第二个列的值，并赋给ddl变量
            ddl = rs.getString(2);
            // 打印ddl变量的值
//            System.out.println(ddl);
        }
//        coon.close();
        return ddl;
    }

    public void getTableComment(String mysqlTable) throws SQLException {

        String ddl = null;
        Statement statement = coon.createStatement();
        String sql = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'ads' ";
        rs = statement.executeQuery(sql);

        if (rs.next()) {
            ResultSetMetaData metaData = rs.getMetaData();
            System.out.println(metaData.getColumnCount());
            for (int i = 1; i <= metaData.getColumnCount(); i++) {
                // 获取当前列名为TABLE_NAME的列值，拿到表名及表注释
                String columnName = metaData.getColumnName(i);


                if ("TABLE_NAME".equalsIgnoreCase(columnName)){
                    String columnValue_tableName = rs.getString(columnName);
                    buff.append("COMMENT ON TABLE ").append(columnValue_tableName);
                    System.out.println(columnName + ": " + columnValue_tableName);
                } else if ("TABLE_COMMENT".equalsIgnoreCase(columnName)) {
                    String columnValue_tableComment = rs.getString(columnName);
                    if (columnValue_tableComment.isEmpty() || columnValue_tableComment == null ) {
                        columnValue_tableComment = "\" \"";
                    }
                    buff.append(" IS ").append(columnValue_tableComment).append(";");
                    System.out.println(columnName + ": " + columnValue_tableComment);
                }
            }

        }
    }

//    public static void main(String[] args) throws SQLException {
//        MysqlDdl ss = new MysqlDdl();
//        ss.getTableDdl("ads_sup_supplier_info");
//    }
}

